from socket import *

# 1. 创建套接字，注意，参数二需改为SOCK_DGRAM
sk = socket(AF_INET, SOCK_DGRAM)

server_addr = ('127.0.0.1', 8080)

# 3. 收发消息，接收所有人的消息
while True:
    try:
        # 给服务器发消息
        msg = input('msg===')
        if not msg:
            break
        sk.sendto(msg.encode(), server_addr)

        # 接收服务器消息
        data, addr = sk.recvfrom(1024)
        print('from server: ', data.decode())
    except Exception as e:
        print(e)
        break

# 4.关闭连接
sk.close()
